from django.shortcuts import render
from django.http import HttpResponse
from utils.db import db





# Create your views here.


#websocket链接
from dwebsocket.decorators import accept_websocket
websocketlist=[]

@accept_websocket
def test(request):
    #   如果是个websocket请求返回True，如果是个普通的http请求返回False,可以用这个方法区分它们。

    if request.is_websocket:
        websocketlist.append(request.websocket)
    print(websocketlist)
    #使用不会报websocket有头标前的错
    for i in request.websocket:
        break

import json   
# #发送消息
# def send():
#     # b1 = request.GET.get('name')
#     # print(b1)
#     b1 = [{'name':'挖打啊打'}]
#     if websocketlist:
#         for i in websocketlist:
#             #把列表转成 字符串
#             b2=json.dumps(b1,ensure_ascii=False).encode('utf8')
#             i.send(b2)

#     return HttpResponse('pk')

import json
import datetime
#发送消息
def send():
    userid = 2
    if websocketlist:
        for i in websocketlist:
            #查询时间和积分
            jflist = []
            timelist = []
            alllist = []
            for j in range(7,0,-1):
                #七天前到现在 7,6,5,4,3,2,1
                day = datetime.datetime.now()-datetime.timedelta(days=j)
                start = datetime.datetime.strftime(day, '%Y-%m-%d')
                timelist.append(start)
                sql = "select tjifen from jifenchange where juserid=%d and ctime like '%s' order by ctime "%(userid,start+'%')
                res = db.find_all(sql)
                jflist.append(res[-1]['tjifen'])
                
            # i.send(jflist,timelist)
            alllist.append(timelist)
            alllist.append(jflist)
            mes = json.dumps(alllist,ensure_ascii=False)
            # print(mes,type(mes))
            i.send(mes)
    # return HttpResponse({'list':alllist})

# def sends():
#     if websocketlist:
#         for i in websocketlist:
#             i.send('222222')
#     return 'lasdasd'

# ['358', '313', '331', '341', '444', '345', '381']
# <class 'str'>
# ['2021-12-29', '2021-12-30', '2021-12-31', '2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04']


#定时任务
from apscheduler.schedulers.background import BackgroundScheduler
def dingshi(request):
    sch = BackgroundScheduler()
    #间隔性任务 每1秒执行一次任务
    sch.add_job(send,'interval',seconds=1)
    #固定性任务 每天15:57分执行
    # sch.add_job(send,trigger='cron',hour=15,minute=57)
    sch.start()
    return 'oasd'



# def haha(request):
#     userid = 2
#     jflist = []
#     timelist = []
#     for i in range(7,0,-1):
#         #七天前到现在 7,6,5,4,3,2,1
#         day = datetime.datetime.now()-datetime.timedelta(days=i)
#         start = datetime.datetime.strftime(day, '%Y-%m-%d')
#         timelist.append(start)
#         sql = "select tjifen from jifenchange where juserid=%d and ctime like '%s' order by ctime "%(userid,start+'%')
#         res = db.find_all(sql)
#         jflist.append(str(res[-1]['tjifen']))
#     print(jflist)
#     print(type(jflist[0]))
#     print(timelist)

#     return HttpResponse('psada')
